<template>
  <div class="common-layout">
    <el-container>
      <el-header height="54px" style="padding: 0">
        <div class="header">
          <div @click="toMySpace" class="logo animate-fade-in">SVG短链平台</div>
          <div style="display: flex; align-items: center" class="header-actions">
            <a
                class="link-span hover-lift"
                style="text-decoration: none"
                target="_blank"
                href=""
            >项目地址</a
            >
            <el-dropdown>
              <div class="block hover-scale">
                <span
                    class="name-span"
                    style="text-decoration: none"
                >{{username}}</span
                >
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="toMine">个人信息</el-dropdown-item>
                  <el-dropdown-item divided @click="logout">退出</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>
      <el-main style="padding: 0">
        <div class="content-box animate-fade-in-up">
          <RouterView class="content-space" />
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { removeKey, removeUsername, getToken, getUsername } from '@/core/auth.js'
import { ElMessage } from 'element-plus'
const { proxy } = getCurrentInstance()
const API = proxy.$API
// 当当前路径和菜单不匹配时，菜单不会被选中
const router = useRouter()
const squareUrl = ref('https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png')
const toMine = () => {
  router.push('/home' + '/account')
}
// 登出
const logout = async () => {
  const token = getToken()
  const username = getUsername()
  // 请求登出的接口
  await API.user.logout({ token, username })
  // 删除cookies中的token和username
  removeUsername()
  removeKey()
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  router.push('/login')
  ElMessage.success('成功退出！')
}
// 点击左上方的图片跳转到我的空间
const toMySpace = () => {
  router.push('/home' + '/space')
}
const username = ref('')
onMounted(async () => {
  const actualUsername = getUsername()
  const res = await API.user.queryUserInfo(actualUsername)
  // firstName.value = res?.data?.data?.realName?.split('')[0]
  username.value = truncateText(actualUsername, 8)
})
const extractColorByName = (name) => {
  var temp = []
  temp.push('#')
  for (let index = 0; index < name.length; index++) {
    temp.push(parseInt(name[index].charCodeAt(0), 10).toString(16))
  }
  return temp.slice(0, 5).join('').slice(0, 4)
}

// 辅助函数，用于截断文本
const truncateText = (text, maxLength) => {
  return text.length > maxLength ? text.slice(0, maxLength) + '...' : text
}
</script>

<style lang="scss" scoped>
.el-container {
  height: 100vh;

  .el-aside {
    border: 0;
    background-color: var(--bg-dark);

    ul {
      border: 0px;
    }
  }

  .el-main {
    background-color: var(--bg-secondary);
  }
}

.header {
  color: var(--text-light);
  background: linear-gradient(135deg, var(--bg-dark), #2a2a2a);
  padding: 0 0 0 24px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border-primary);
  box-shadow: var(--shadow-light);
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
    animation: shimmer 4s ease-in-out infinite;
  }

  .header-actions {
    animation: slideInRight var(--animation-slow) var(--ease-out) 0.3s both;
  }

  .block {
    cursor: pointer;
    display: flex;
    align-items: center;
    border: 0px;
    transition: all var(--animation-normal) var(--ease-out);
    position: relative;
    overflow: hidden;
    
    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: -100%;
      width: 100%;
      height: 100%;
      background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.1), transparent);
      transition: left 0.5s ease;
    }
    
    &:hover::before {
      left: 100%;
    }
    
    &:hover {
      opacity: 0.8;
      transform: translateY(-1px);
    }
  }
}

.content-box {
  height: calc(100vh - 54px);
  background: linear-gradient(135deg, var(--bg-primary), var(--bg-secondary));
  border-radius: var(--radius-large) var(--radius-large) 0 0;
  margin-top: 1px;
  box-shadow: var(--shadow-light);
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: linear-gradient(135deg, transparent 0%, rgba(26, 26, 26, 0.02) 50%, transparent 100%);
    background-size: 200% 200%;
    animation: gradientShift 8s ease infinite;
    pointer-events: none;
  }
}

:deep(.el-tooltip__trigger:focus-visible) {
  outline: unset;
}

.logo {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-light);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  cursor: pointer;
  letter-spacing: 1px;
  transition: all var(--animation-normal) var(--ease-out);
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s ease;
  }
  
  &:hover::before {
    left: 100%;
  }
  
  &:hover {
    color: var(--text-light);
    transform: translateY(-1px);
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
  }
}

.link-span {
  color: var(--text-light);
  opacity: 0.8;
  margin-right: 32px;
  font-size: 14px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  cursor: pointer;
  text-decoration: none;
  transition: all var(--animation-normal) var(--ease-out);
  font-weight: 500;
  position: relative;
  overflow: hidden;
  
  &::before {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 0;
    height: 1px;
    background: var(--text-light);
    transition: width var(--animation-normal) var(--ease-out);
  }
  
  &:hover {
    text-decoration: underline !important;
    opacity: 1;
    color: var(--text-light);
    transform: translateY(-1px);
    
    &::before {
      width: 100%;
    }
  }
}

.name-span {
  color: var(--text-light);
  opacity: 0.8;
  margin-right: 32px;
  font-size: 13px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  cursor: pointer;
  text-decoration: none;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-weight: 500;
  transition: all var(--animation-normal) var(--ease-out);
  position: relative;
  
  &::after {
    content: '';
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 0;
    height: 2px;
    background: var(--text-light);
    transition: width var(--animation-normal) var(--ease-out);
  }
  
  &:hover {
    opacity: 1;
    
    &::after {
      width: 100%;
    }
  }
}

.avatar {
  transform: translateY(-2px);
}

// 科技风格装饰元素
.header::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent 0%, rgba(255, 255, 255, 0.1) 50%, transparent 100%);
}

// 下拉菜单样式
:deep(.el-dropdown-menu) {
  border-radius: var(--radius-medium);
  border: 1px solid var(--border-primary);
  box-shadow: var(--shadow-medium);
  
  .el-dropdown-menu__item {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
    color: var(--text-primary);
    transition: all 0.3s ease;
    
    &:hover {
      background-color: var(--bg-secondary);
      color: var(--el-color-primary);
    }
  }
}
</style>
